package com.micro.frog.acct.rest;

import com.micro.frog.acct.dto.MenuDTO;
import com.micro.frog.acct.dto.UserDetail;
import com.micro.frog.acct.entity.User;
import com.micro.frog.acct.utils.CacheUtil;
import com.micro.frog.acct.utils.SubjectUtil;
import com.micro.frog.acct.vo.MenuVO;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.Result;
import com.micro.frog.acct.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *  菜单加载类
 * @author :  ljh
 * @date : 2018/4/2
 **/
@Slf4j
@RestController
@RequestMapping(value = "/rbac/menu")
public class MenuController {

    @Autowired
    MenuService menuService;

    @GetMapping(value = "user")
    public Result userMenu(HttpServletRequest request){
        Result result = new Result();
        try{
            UserDetail userDetail = SubjectUtil.getUser(request);
            if (userDetail == null){
                result.setFlag(false);
                result.setMsg("用户未登录");
                return  result;
            }
//            if (CollectionUtils.isEmpty(userDetail.getMenus())){
//                User user = new User();
//                BeanUtils.copyProperties(userDetail,user);
//                menuService.userMenus(user);
//            }
            result.setFlag(true);
            result.setMsg("成功");
            result.setData(userDetail.getMenus());
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("系统异常");
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping(value = "/all")
    public Result all(){
        Result result = new Result();
        try{
            result.setFlag(true);
            result.setMsg("成功");
            result.setData(menuService.queryAll());
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("系统异常");
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping(value = "/list")
    public PageResult list(MenuDTO queryVO){
        PageResult result = new PageResult();
        try{
            result = menuService.list(queryVO);

        }catch (Exception e){
            result.setCode("20000");
            result.setMsg("系统异常");
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/add")
    public Result addMenu(MenuVO menuVO) {
        Result result = new Result();
        try{
            menuService.addMenu(menuVO);
            result.setFlag(true);
            result.setMsg("新增成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("新增失败,系统异常");
        }
        return result;
    }

    @PostMapping("/delete")
    public Result deleteMenu(Integer menuId) {
        Result result = new Result();
        try{
            menuService.deleteMenu(menuId);
            result.setFlag(true);
            result.setMsg("删除成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("删除失败,系统异常");
        }
        return result;
    }

    @PostMapping("/update")
    public Result updateMenu(MenuVO menuVO) {
        Result result = new Result();
        try{
            menuService.updateMenu(menuVO);
            result.setFlag(true);
            result.setMsg("修改成功");
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("修改失败,系统异常");
        }
        return result;
    }

    @GetMapping("/tree")
    public Result tree() {
        Result result = new Result();
        try{
            result.setFlag(true);
            result.setMsg("成功");
            result.setData(menuService.tree());
        }catch (Exception e){
            result.setFlag(false);
            result.setMsg("失败");
            log.error("失败，e={}", e);
        }
        return result;
    }
}
